package com.ht.managermentserver.handler;

import com.ht.managermentserver.sysenum.ErrorCode;
import com.ht.managermentserver.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 全局异常处理
 *
 * @author czy
 */
@ControllerAdvice
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

  @ExceptionHandler(value = {IllegalArgumentException.class})
  @ResponseStatus(HttpStatus.BAD_REQUEST)
  @ResponseBody
  public void illegalArgumentException(
      final HttpServletRequest req, final HttpServletResponse res, final IllegalArgumentException e)
      throws IOException {
    final String errorMessage = e.getMessage() == null ? "BAD_REQUEST" : e.getMessage();
    log.error(errorMessage, e);
    res.setCharacterEncoding("UTF-8");
    res.setContentType("text/json;charset=utf-8");
    String resbody = "";
    try {
      // 请求地址
      final String url = req.getRequestURI();
      resbody = JsonResult.create(errorMessage, ErrorCode.E500, url).toJsonString();

    } catch (final Exception ex) {
      resbody = JsonResult.create(ex.getMessage(), ErrorCode.E500).toJsonString();
    }
    res.getWriter().write(resbody);
  }

  @ExceptionHandler(value = {NoHandlerFoundException.class})
  @ResponseStatus(HttpStatus.NOT_FOUND)
  @ResponseBody
  public void noHandlerFoundException(
      final HttpServletRequest req, final HttpServletResponse res, final Exception e)
      throws IOException {
    final String errorMessage = e.getMessage() == null ? "NOT_FOUND" : e.getMessage();
    log.error(errorMessage, e);
    res.setCharacterEncoding("UTF-8");
    res.setContentType("text/json;charset=utf-8");
    String resbody = "";
    try {
      // 请求地址
      final String url = req.getRequestURI();
      resbody = JsonResult.create(errorMessage, ErrorCode.E404, url).toJsonString();

    } catch (final Exception ex) {
      resbody = JsonResult.create(ex.getMessage(), ErrorCode.E500).toJsonString();
    }
    res.getWriter().write(resbody);
  }

  @ExceptionHandler(value = {NullPointerException.class})
  @ResponseBody
  public void nullPointerException(
      final HttpServletRequest req, final HttpServletResponse res, final Exception e)
      throws IOException {

    final String errorMessage = e.getMessage() == null ? "空指针异常" : e.getMessage();
    log.error(errorMessage, e);
    res.setCharacterEncoding("UTF-8");
    res.setContentType("text/json;charset=utf-8");
    String resbody = "";
    try {
      // 请求地址
      final String url = req.getRequestURI();
      resbody = JsonResult.create(errorMessage, ErrorCode.E500, url).toJsonString();

    } catch (final Exception ex) {
      resbody = JsonResult.create(ex.getMessage(), ErrorCode.E500).toJsonString();
    }
    res.getWriter().write(resbody);
  }

  @ExceptionHandler(value = Exception.class)
  @ResponseBody
  @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  public void handlerAllException(
      final HttpServletRequest req, final HttpServletResponse res, final Exception e)
      throws IOException {
    final String errorMessage = e.getMessage() == null ? "INTERNAL_SERVER_ERROR" : e.getMessage();
    log.error(errorMessage, e);
    res.setCharacterEncoding("UTF-8");
    res.setContentType("text/json;charset=utf-8");
    String resbody = "";
    try {
      // 请求地址
      final String url = req.getRequestURI();
      resbody = JsonResult.create(errorMessage, ErrorCode.E500, url).toJsonString();

    } catch (final Exception ex) {
      resbody = JsonResult.create(ex.getMessage(), ErrorCode.E500).toJsonString();
    }
    res.getWriter().write(resbody);
  }
}
